﻿@page "/cart"
@inject ICartService _cartService


<PageTitle>Shopping Cart</PageTitle>

<h3>Shopping Cart</h3>

@if (cartProducts == null || cartProducts.Count == 0)
{
    <span>@message</span>
}
else
{
    <div>
        @foreach (var product in cartProducts)
        {
            <div class="container">
                <div class="image-wrapper">
                    <img src="@product.ImageUrl" class="image" />
                </div>
                <div class="name">
                    <h5><a href="/product/@product.ProductId">@product.Title</a></h5>
                    <span>@product.ProductType</span><br/>
                    <input type="number" value="@product.Quantity" @onchange="@((ChangeEventArgs e)=>UpdateQuantity(e,product))" class="form-control input-quantity" min="1"/>

                    <button class="btn-delete" @onclick="@(() => RemoveProductFromCart(product.ProductId,product.ProductTypeId))">Delete</button>
                    
                </div>
                <div class="cart-product-price">$(@product.Price * @product.Quantity)</div>
            </div>
        }
        <div class="cart-product-price">
            Total (@cartProducts.Count):$@cartProducts.Sum(product=>@product.Price* @product.Quantity)
        </div>
    </div>
}

@code {

    // 购物车中的商品信息
    List<CartProductResponse> cartProducts = null;
    // 页面提示内容
    string message = "Loading cart...";

    protected override async Task OnInitializedAsync()
    {
        await LoadCart();
    }

    //从购物车中移除商品   
    private async Task RemoveProductFromCart(int productId,int productTypeId)
    {
        await _cartService.RemoveProductFromCart(productId, productTypeId);
        await LoadCart();
    }

    private async Task LoadCart()
    {
        if ((await _cartService.GetCartItems()).Count == 0)
        {
            message = "Your cart is empty.";
            cartProducts = new List<CartProductResponse>();
        }
        else
        {
            cartProducts = await _cartService.GetCartProducts();
        }
    }

    private async Task UpdateQuantity(ChangeEventArgs e,CartProductResponse product)
    {
        product.Quantity = int.Parse(e.Value.ToString());
        if(product.Quantity< 1)
        {
            product.Quantity = 1;
        }
        await _cartService.UpdateQuantity(product);

    }

}
